package cc.yugu.yhdw.webserver.web.controller.security.basic;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrder;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrderBatteryReportLog;
import cc.yugu.yhdw.common.entity.LocationInfo;
import cc.yugu.yhdw.common.entity.json.DataResult;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.webserver.service.hdw.BatteryReportLogService;
import cc.yugu.yhdw.webserver.service.hdw.BatteryService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeOrderBatteryReportLogService;
import cc.yugu.yhdw.webserver.service.hdw.ExchangeOrderService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
/**
 * 百度地图控制器
 * 
 * @Date:Created in 2018/7/17 13:17
 * @Author:
 * @Modified By: zjn 2018/7/17 13:17
 */ 
@Controller
@RequestMapping(value = "/security/basic/baidu_map")
public class BaiduMapController extends SecurityController {
    @Autowired
    BatteryService batteryService;
    @Autowired
    ExchangeOrderService exchangeOrderService;
    @Autowired
    BatteryReportLogService batteryReportLogService;
    @Autowired
    ExchangeOrderBatteryReportLogService exchangeOrderBatteryReportLogService;
    /**
    * 保存数据，返回到location.ftl显示
    * 
    * @Param: [model, address, lng, lat, readonly]
    * @Return: void
    * @Author By: zjn 2018/7/17  13:21
    */ 
    @RequestMapping(value = "location.htm")
    public void location(Model model, String address, Double lng, Double lat, Integer readonly) {
        model.addAttribute("address", address);
        model.addAttribute("lng", lng);
        model.addAttribute("lat", lat);
        model.addAttribute("readonly", readonly);
    }
    /**
    * 保存经纬度，返回到battery_location.ftl显示
    * 
    * @Param: [model, lng, lat]
    * @Return: void
    * @Author By: zjn 2018/7/17  13:21
    */ 
    @RequestMapping(value = "battery_location.htm")
    public void battery_location(Model model, Double lng, Double lat) {
        model.addAttribute("lng", lng);
        model.addAttribute("lat", lat);
    }
    /**
    * 换电订单页面点击轨迹跳到这里，
    * 
    * @Param: []
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/17  13:30
    */ 
    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "view_map.htm")
    public String viewMap() {
        return "/security/basic/baidu_map/view_map";
    }
    /**
    * 跳到twinkle_location.ftl
    * 电池分布图
    * @Param: []
    * @Return: void
    * @Author By: zjn 2018/7/17  13:31
    */
    @RequestMapping(value = "twinkle_location.htm")
    public void twinkle_location() {
    }
    /**
     * 跳到cabinet_twinkle_location.ftl
     * 柜机分布图
     * @Param []
     * @Return void
     * @Author zxj 2018/8/28
     */
    @RequestMapping(value = "cabinet_twinkle_location.htm")
    public void cabinet_twinkle_location() {
    }
    /**
    * 查询定位
    *
    * @Param: [orderId, startDate, endDate]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/17  13:31
    */
    @ResponseBody
    @RequestMapping("map_data.htm")
    @ViewModel(ViewModel.JSON)
    public ExtResult mapData(String orderId, Date startDate, Date endDate) {
        // 查询换电订单表
        ExchangeOrder entity = exchangeOrderService.find(orderId);
        if (StringUtils.isNotEmpty(orderId) && entity.getCreateTime() != null) {
            // 初始订单轨迹定位业务查询
            List<LocationInfo> list = exchangeOrderBatteryReportLogService.findAllMap(orderId, entity.getCreateTime(), startDate, endDate);
            return DataResult.successResult(list);
        } else {
            return DataResult.failResult("");
        }
    }
}
